package com.br4mmie.asciistory.mission;

import java.io.*;

import com.br4mmie.asciistory.CharImage;
import com.br4mmie.asciistory.FileGetter;
import com.br4mmie.asciistory.map.MapObject;
import com.br4mmie.asciistory.ui.TextWindow;


/**
 * @author Pim K�rschner
 * 		   Rohmin Mirza 		   
 *		   Ken Veenstra
 */


/**
 * Item is pretty self explanatory, weapons and armor will need names, 
 * descriptions and a damage or defence value.
 * 
 * keys only need names and descriptions
 * 
 * Healthpacks and Gold need values, 
 * for the health pack the value represents the amount of health that will
 * be gained, for Gold it will represent the amount of gold.
 * 
 * Weapon, Armor, Key, Gold are all subclasses of this class.
 * 
 */
public class Item
{
	protected 	String 		name;			//name of the item
	protected 	String 		description;	//the description of the item
	private 	String 		type;			//type of weapon
	private 	MapObject 	mapObject;
	private 	CharImage 	itemImage;		//image on the map
	private 	int 		gold;			//value of the weapon in the shop
	BufferedReader 			file;
	
	//constructor used for healthpacks and gold
	public Item(CharImage itemImage)
	{
		this.itemImage = itemImage;
	}
	
	public Item getItem()
	{
		return this;
	}
	
	static public Item fromFile(String filename) throws NumberFormatException,  IOException //loads a Item from a file and makes a weapon or armor from it
	{
		filename = "data/items/" + filename;
		BufferedReader file = new BufferedReader(new FileReader(FileGetter.getFilename(filename)));
		
		while(true)
		{
			String line = file.readLine();
			if(line == null) break;
			
			if	(line.startsWith("w "))
			{
				return Weapon.fromFile(line, file);
			}
			else if (line.startsWith("a "))
			{
				return Armor.fromFile(line, file);
			}
			else if (line. startsWith("h "))
			{
				return ConsumableItem.fromFile(line, file);
			}
			else if (line. startsWith("x "));
			{
				return RemainingItems.fromFile(line, file);
			}
		}
		return null;
	}
	
	//constructor for the other items that need atleast a name and description
	public Item(String name, String description, CharImage itemImage, int gold, String type)
	{
		this.name = name;
		this.description = description;
		this.itemImage = itemImage;
		this.gold	= gold;
		this.type	= type;
	}
	
	public boolean use()
	{
		TextWindow window = new TextWindow(2, 2, 26, 10, 0, true);
		window.addParagraph(description);
		window.addParagraph("<press X to close>");
		
		return false;
	}

	public CharImage getImage()
	{
		return itemImage;
	}
	
	public String getName()
	{
		return name;
	}
	
	public String getType()
	{
		return type;
	}
	
	public String getDescription()
	{
		return description;
	}
	
	public int getGoldValue()
	{
		return gold;
	}
	
	public MapObject createMapObject(int x, int y)
	{
		mapObject = new ItemMapObject(this);
		mapObject.setX(x);
		mapObject.setY(y);
		mapObject.setImage(itemImage);
		
		return mapObject;
	}
	
	public MapObject getMapObject()
	{
		return mapObject;
	}
}